import xlrd
import xlwt


def read_excel(excel_path):
    """
    根据文件路径读取excel文件
    :param excel_path:excel文件路径
    :return:excel文件数据列表
    """
    data = xlrd.open_workbook(excel_path)  # excel 文件对象
    # sheet_names = data.sheet_names()
    # table = data.sheets()[0]  # 通过索引顺序获取
    # table = data.sheet_by_index(0)  # 通过索引顺序获取
    # table = data.sheet_by_name(u'Sheet1')  # 通过名称获取
    # sheet : 表格 sheet = data.sheets()[0]
    excel_list = []
    for sheet in data.sheets():
        sheet_list = []
        # 循环Excel文件的所有行
        for row in sheet.get_rows():
            sheet_list.append(row)

        excel_list.append(sheet_list)
    return excel_list
            # # 循环一行的所有列
            # for col in row:
            #     # 获取一个单元格中的值
            #     print(col.value)


def write_excel(excel_path, data_list):
    """
    写入excel 文件
    :param excel_path:excel 文件路径
    :param data_list: excel数据列表
    :return:None
    """
    wb = xlwt.Workbook()
    for sheet_i in range(len(data_list)):
        sheet = wb.add_sheet('sheet{}'.format(sheet_i + 1))  # 给workbook对象添加一张表 sheet1
        for row_i in range(len(data_list[sheet_i])):
            for col_i in range(len(data_list[sheet_i][row_i])):
                value = data_list[sheet_i][row_i][col_i]
                sheet.write(row_i, col_i, value)

    wb.save(excel_path)


if __name__ == '__main__':

    excel_path = "C:\\Users\\Administrator\\Desktop\\淘宝 cookie分析.xls"
    data_list = read_excel(excel_path)


    excel_path = "C:\\Users\\Administrator\\Desktop\\a.xls"
    write_excel(excel_path, data_list)